﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public SerialPort serial ;
        public Client client;
        public List<Chatmessage> chat = new List<Chatmessage>();
        public Form1(Client c,  SerialPort serialPort)
        {
            
            InitializeComponent();
            serial = serialPort;
            client = c;            

            serial.WriteLine("CD");                        
            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                while (true)
                {
                     serial.WriteLine("ST");
                    string s = serial.ReadLine();                  
                    client.routeToServer(s);
                    sendMeasurement(s);
                    updateLabels(s);                   
                    System.Threading.Thread.Sleep(1000);
                }
            });
        }

        public void sendMeasurement(String s)
        {
            string[] list = s.Split(new string[] { "	" }, StringSplitOptions.None);

            if (list.Length != 8)
            {
                return;
            }
            String[] Naam = list[6].Split(':');
            client.routeMeasurementToServer(new lib.MeasuredValues(Convert.ToInt32(list[0]), Convert.ToInt32(list[1]), Convert.ToInt32(list[2]),
                                            Convert.ToInt32(list[3]), Convert.ToInt32(list[4]), Convert.ToInt32(list[5]), 
                                            new DateTime(2013, 10, 23,0,Convert.ToInt32(Naam[0]),Convert.ToInt32(Naam[1])), Convert.ToInt32(list[7])));
            System.Diagnostics.Debug.WriteLine(list[6]);
        }

        public void updateLabels(string s)
        {
            try
            {
                if (s.Length > 10 && this.IsHandleCreated)
                {
                    string[] list = s.Split(new string[] { "	" }, StringSplitOptions.None);
                    label8.Invoke(new MethodInvoker(delegate
                    {
                        label8.Text = list[0];
                    }));
                    label9.Invoke(new MethodInvoker(delegate
                    {
                        label9.Text = list[1];
                    }));
                    label10.Invoke(new MethodInvoker(delegate
                    {
                        label10.Text = Convert.ToDouble(list[2]) / 10 + "  Km/h";
                    }));
                    label11.Invoke(new MethodInvoker(delegate
                    {
                        label11.Text = Convert.ToDouble(list[3]) / 10 + "  Km";
                    }));
                    label12.Invoke(new MethodInvoker(delegate
                    {
                        label12.Text = list[4];
                    }));
                    label13.Invoke(new MethodInvoker(delegate
                    {
                        label13.Text = list[5] + " Kj";
                    }));
                    label14.Invoke(new MethodInvoker(delegate
                    {
                        label14.Text = list[6];
                    }));
                    listBox1.Invoke(new MethodInvoker(delegate
                    {
                        if (listBox1.Items.Count < client.getChat().Count)
                        {
                            for (int i = listBox1.Items.Count; i < client.getChat().Count; i++)
                            {
                                listBox1.Items.Add(client.getChat()[i]);
                                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                            }
                        }
                    }));
                }
            }
            catch { }
        }       

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            client.routeToServer("data");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Button1_MouseDown(object sender, MouseEventArgs e)
        {
            messageBox.Text = messageBox.Text.Replace(':', ' ');
            client.routeToServer("chat"+messageBox.Text);
            chat.Add(new Chatmessage(true, messageBox.Text));
            messageBox.Clear();
        }

        private List<string> toStringList(List<Chatmessage> list)
        {
            List<string> result = new List<string>();
            foreach (Chatmessage c in chat)
            {
                result.Add(c.toString());
            }
            return result;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sentMessage(messageBox.Text);
        }

        private void sentMessage(String s)
        {
            if (s != "")
            {
                client.sendMessage(s);
                messageBox.Text = "";
                listBox1.SelectedIndex = listBox1.Items.Count - 1; 
            }
        }

        private void messageBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                messageBox.Text = messageBox.Text.Replace(':', ' ');
                sentMessage(messageBox.Text);
            }
        }

    }
}
